package com.echat.api.demo.serviceapi.simpleCode;

import com.alibaba.fastjson.JSON;
import com.echat.api.demo.serviceapi.config.EchatServiceApiConfig;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

/**
 * Created by chenmohan on 2018/09/13
 *
 * @description   简单请求accessToken的样例，注意【1】【2】【3】
 */
public class SimpleDemo {

    private static final String accessKey = EchatServiceApiConfig.accessKey;
    private static final String accessSecret = EchatServiceApiConfig.accessSecret;

    /**
     * 使用OkHttp发送Http请求
     */
    public static void main(String[] args) throws InterruptedException {

        String accessToken = null;
        long ttl = -1;

        //【1】获取accessToken
        String getTokenUrl = EchatServiceApiConfig.host+"/auth/accquire";
        OkHttpClient client = new OkHttpClient();
        FormBody formBody = new FormBody.Builder()
                .add("accessKey", accessKey)
                .add("accessSecret", accessSecret)
                .build();
            //使用的POST方式请求accessToken,GET方式也可以
        Request getTokenRequest = new Request.Builder().post(formBody).url(getTokenUrl).build();
        try {
            Response tokenResponse = client.newCall(getTokenRequest).execute();
            String result = tokenResponse.body().string();
            //拿到accessToken,除了accessToken外，还有其他结果字段,详细查看说明文档
            accessToken = (String) JSON.parseObject(result).get("accessToken");
            tokenResponse.body().close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("accessToken: " + accessToken);

        //【2】查询token的TTL
        String getTTLUrl = EchatServiceApiConfig.host+"/auth/ttl?accessToken="+accessToken;
        Request getTTLRequest = new Request.Builder()
                .url(getTTLUrl)
                .get().build();
        try {
            Response ttlResponse = client.newCall(getTTLRequest).execute();
            String result = ttlResponse.body().string();
            ttl = Long.valueOf(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("ttl: " + ttl);

        //【3】不断的获取token的TTL并判断过期时间是否小于设置的阈值,如果小于设置的阈值,那么就重新获取token
        long threshold = 1000;

        while(true){
            if(ttl<threshold){
                try {
                    //重新获取accessToken
                    Response tokenResponse = client.newCall(getTokenRequest).execute();
                    String result = tokenResponse.body().string();
                    accessToken = (String) JSON.parseObject(result).get("accessToken");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            //获取新的TTL
            getTTLUrl = EchatServiceApiConfig.host+"/auth/ttl?accessToken="+accessToken;
            getTTLRequest = new Request.Builder()
                    .url(getTTLUrl)
                    .get().build();
            try {
                Response ttlResponse = client.newCall(getTTLRequest).execute();
                String result = ttlResponse.body().string();
                ttl = Long.valueOf(result);
            } catch (IOException e) {
                e.printStackTrace();
            }

            Thread.sleep(60*1000);  //每隔1分钟获取1次ttl,根据ttl来判断是否需要获取新的token
        }
    }


}
